<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

/**
 * HTML View class for the View
 */
class TaxiBookingViewTaxiBooking extends JView
{

    /**
     * Creates the Forms for the View
     *
     * @since 1.0
     */
    function display($tpl = null)
    {

        $app = JFactory::getApplication();
        
        //initialize variables
        $document = & JFactory::getDocument();
        $menus = $app->getMenu();
        $lang = & JFactory::getLanguage();
        $menu = $menus->getActive();
        $uri = & JFactory::getURI();
        $session = & JFactory::getSession();
        $elsettings = & booking_helper::config();
		$acl		=  JFactory::getACL();

        // Get the page/component configuration

        $params = $app->getParams();
//		$params = $mainframe->getParams('com_taxibooking');
        // Load the toolbar helper
        require_once( JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'toolbar.php' );

        //add css file
        $document->addStyleSheet($this->baseurl . '/components/com_taxibooking/assets/css/booking.css');

        // right from the menu item itself
        if (is_object($menu))
        {
            $menu_params = new JRegistry;
            $menu_params->loadString($menu->params);

            if (!$menu_params->get('page_title'))
            {
                $params->set('page_title', JText::_('Taxi Booking'));
            }
        }
        else
        {
            $params->set('page_title', JText::_('Taxi Booking'));
        }

        /*
         * Handle the metadata for the categories list
         */
//        $document->setTitle($params['page_title']);
//        $document->setMetadata('keywords', $params['page_title']);
        $document->setTitle($title);
        $document->setMetadata('keywords', $params->get('page_title'));

        if ($app->getCfg('MetaTitle') == '1')
        {
            $document->setMetadata('title', $params->get('page_title'));
        }


        $from_dest = $session->get('from_dest', 0);
        $airports1 = $session->get('airports1', 0);
        $postcode1 = $session->get('postcode1', '');
        $district1 = $session->get('district1', '');
        $town1 = $session->get('town1', '');
        $street1 = $session->get('street1', '');
        $country1 = $session->get('country1', '');

        $to_dest = $session->get('to_dest', 0);
        $airports2 = $session->get('airports2', 0);
        $postcode2 = $session->get('postcode2', '');
        $district2 = $session->get('district2', '');
        $town2 = $session->get('town2', '');
        $street2 = $session->get('street2', '');
        $country2 = $session->get('country2', '');

        $returnjurney = $session->get('returnjurney', 0);
        $vehicle = $session->get('vehicle', 0);

        $from_error = $session->get('from_error', '');
        $to_error = $session->get('to_error', '');
        $zip_error = $session->get('zip_error', '');
        $passenger_error = $session->get('passenger_error', '');

        //Get the lists
        $lists = $this->_buildEditLists();

        $this->assign('action', $uri->toString());

        $this->assignRef('elsettings', $elsettings);
        $this->assignRef('from_dest', $from_dest);
        $this->assignRef('airports1', $airports1);
        $this->assignRef('postcode1', $postcode1);
        $this->assignRef('district1', $district1);
        $this->assignRef('town1', $town1);
        $this->assignRef('street1', $street1);
        $this->assignRef('country1', $country1);
        $this->assignRef('to_dest', $to_dest);
        $this->assignRef('airports2', $airports2);
        $this->assignRef('postcode2', $postcode2);
        $this->assignRef('district2', $district2);
        $this->assignRef('town2', $town2);
        $this->assignRef('street2', $street2);
        $this->assignRef('country2', $country2);
        $this->assignRef('returnjurney', $returnjurney);
        $this->assignRef('vehicle', $vehicle);
        $this->assignRef('from_error', $from_error);
        $this->assignRef('to_error', $to_error);
        $this->assignRef('zip_error', $zip_error);
        $this->assignRef('passenger_error', $passenger_error);
        $this->assignRef('lists', $lists);


        parent::display($tpl);
    }

    /**
     * Creates the item submit form
     *
     * @since 1.0
     */
    function _buildEditLists()
    {
        $session = & JFactory::getSession();
        $elsettings = & booking_helper::config();

        $db = & JFactory::getDBO();

        $airports1 = $session->get('airports1', '');
        $seaports1 = $session->get('seaports1', '');
        $county1 = $session->get('county1', '');
        $district1 = $session->get('district1', '');
        $postcode1 = $session->get('postcode1', '');
        $airports2 = $session->get('airports2', '');
        $seaports2 = $session->get('seaports2', '');
        $county2 = $session->get('county2', '');
        $postcode2 = $session->get('postcode2', '');
        $district2 = $session->get('district2', '');
        $passengers = $session->get('passengers', 0);
        $infantseats = $session->get('infantseats', 0);
        $chseats = $session->get('chseats', 0);
        $boosterseats = $session->get('boosterseats', 0);
        $suitcases = $session->get('suitcases', 0);

        //build selectlist
        $lists = array();

        $airports = array();
        $airports[] = JHTML::_('select.option', '', JText::_('SELECT PLACE'));

        // get all the points of interest
        $query = 'SELECT *'
                . ' FROM #__taxibooking_points'
                . ' WHERE published = 1'
                . ' ORDER BY ordering ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $places = $db->loadObjectList();

        if (!empty($places))
        {
            foreach ($places as $place)
            {
                $airports[] = JHTML::_('select.option', $place->id, $place->title);
            }
        }
        /* $airports[] = JHTML::_('select.option',  'London Heathrow Airport', 'London Heathrow Airport (LHR)' );
          $airports[] = JHTML::_('select.option',  'London Gatwick airport', 'London Gatwick airport (LGW)' );
          $airports[] = JHTML::_('select.option',  'London Luton Airport', 'London Luton Airport (LTN)' );
          $airports[] = JHTML::_('select.option',  'London Stansted Airport', 'London Stansted Airport (STN)' );
          $airports[] = JHTML::_('select.option',  'London City Airport', 'London City Airport (LCY)' ); */


        $lists['airports1'] = JHTML::_('select.genericlist', $airports, 'airports1', 'style="font-size:12px; width:130px"', 'value', 'text', $airports1);
        $lists['airports2'] = JHTML::_('select.genericlist', $airports, 'airports2', 'style="font-size:12px; width:130px"', 'value', 'text', $airports2);

        // get all the county
        $query = 'SELECT *'
                . ' FROM #__taxibooking_county'
                . ' WHERE published = 1'
                . ' ORDER BY ordering ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $countys = $db->loadObjectList();

        $county_options = array();
        $county_options[] = JHTML::_('select.option', '', JText::_('SELECT COUNTY'));
        if (!empty($countys))
        {
            foreach ($countys as $county)
            {
                $county_options[] = JHTML::_('select.option', urlencode($county->title), $county->title);
            }
        }

        $lists['county1'] = JHTML::_('select.genericlist', $county_options, 'county1', 'style="font-size:12px; width:130px"', 'value', 'text', $county1);
        $lists['county2'] = JHTML::_('select.genericlist', $county_options, 'county2', 'style="font-size:12px; width:130px"', 'value', 'text', $county2);

        // get the county id
        $query = 'SELECT id'
                . ' FROM #__taxibooking_county'
                . ' WHERE title = ' . $db->quote(urldecode($county1))
        ;
        $db->setQuery($query);
        $db->query();
        $county_id1 = $db->loadResult();

        // get all the district
        $query = 'SELECT *'
                . ' FROM #__taxibooking_district'
                . ' WHERE county_id = ' . $county_id1
                . ' AND published = 1'
                . ' ORDER BY ordering ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $districts = $db->loadObjectList();

        $district_options = array();
        $district_options[] = JHTML::_('select.option', '', JText::_('SELECT DISTRICT'));
        if (!empty($districts))
        {
            foreach ($districts as $district)
            {
                $district_options[] = JHTML::_('select.option', $district->title, $district->title);
            }
        }

        $lists['districts1'] = JHTML::_('select.genericlist', $district_options, 'district1', 'style="font-size:12px; width:130px"', 'value', 'text', $district1);

        // get the county id
        $query = 'SELECT id'
                . ' FROM #__taxibooking_county'
                . ' WHERE title = ' . $db->quote(urldecode($county2))
        ;
        $db->setQuery($query);
        $db->query();
        $county_id2 = $db->loadResult();

        // get all the district
        $query = 'SELECT *'
                . ' FROM #__taxibooking_district'
                . ' WHERE county_id = ' . $county_id2
                . ' AND published = 1'
                . ' ORDER BY ordering ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $districts = $db->loadObjectList();

        $district_options = array();
        $district_options[] = JHTML::_('select.option', '', JText::_('SELECT DISTRICT'));
        if (!empty($districts))
        {
            foreach ($districts as $district)
            {
                $district_options[] = JHTML::_('select.option', $district->title, $district->title);
            }
        }
        $lists['districts2'] = JHTML::_('select.genericlist', $district_options, 'district2', 'style="font-size:12px; width:130px"', 'value', 'text', $district2);

        $seaports = array();
        $seaports[] = JHTML::_('select.option', '', 'Please select a Seaport');
        $seaports[] = JHTML::_('select.option', 'Dover Port', 'Dover Port');
        $seaports[] = JHTML::_('select.option', 'Harwich Curiseport', 'Harwich Curiseport');
        $seaports[] = JHTML::_('select.option', 'Portsmouth Curiseport', 'Portsmouth Curiseport');
        $seaports[] = JHTML::_('select.option', 'Southampton Curiseport', 'Southampton Curiseport');


        $lists['seaports1'] = JHTML::_('select.genericlist', $seaports, 'seaports1', 'style="font-size:12px; width:130px"', 'value', 'text', $seaports1);
        $lists['seaports2'] = JHTML::_('select.genericlist', $seaports, 'seaports2', 'style="font-size:12px; width:130px"', 'value', 'text', $seaports2);

        // get the fixed routes option
        $fixed_route = $session->get('fixed_route', '');
        $query = 'SELECT *'
                . ' FROM #__taxibooking_routes'
                . ' WHERE published = 1'
                . ' ORDER BY ordering ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $routes = $db->loadObjectList();

        $route_options = array();
        $route_options[] = JHTML::_('select.option', '', JText::_('SELECT ROUTE'));
        $lists['has_any_route'] = false;
        if (!empty($routes))
        {
            $lists['has_any_route'] = true;
            foreach ($routes as $route)
            {
                $route_options[] = JHTML::_('select.option', $route->id, $route->title.' - '.$elsettings->currency_symbol.$route->price);
            }
        }
        $lists['fixed_routes'] = JHTML::_('select.genericlist', $route_options, 'fixed_route', 'style="font-size:12px; width:130px"', 'value', 'text', $fixed_route);
        
        
        $passenger_options = array();
        for ($i = 0; $i <= 8; $i++)
        {
            $passenger_options[] = JHTML::_('select.option', $i, $i);
        }
        $lists['passengers'] = JHTML::_('select.genericlist', $passenger_options, 'passengers', 'onChange="check_pass(this.value)"', 'value', 'text', $passengers);

        $chseats_options = array();
        for ($i = 0; $i <= 5; $i++)
        {
            $chseats_options[] = JHTML::_('select.option', $i, $i);
        }
        $lists['infantseats'] = JHTML::_('select.genericlist', $chseats_options, 'infantseats', 'onChange="check_pass(document.getElementById(\'passengers\').value)"', 'value', 'text', $infantseats);
        $lists['chseats'] = JHTML::_('select.genericlist', $chseats_options, 'chseats', 'onChange="check_pass(document.getElementById(\'passengers\').value)"', 'value', 'text', $chseats);
        $lists['boosterseats'] = JHTML::_('select.genericlist', $chseats_options, 'boosterseats', 'onChange="check_pass(document.getElementById(\'passengers\').value)"', 'value', 'text', $boosterseats);


        $ch_seat_types = array();
        $ch_seat_types[] = JHTML::_('select.option', 0, '- Select -');
        $ch_seat_types[] = JHTML::_('select.option', 1, 'Infant seat');
        $ch_seat_types[] = JHTML::_('select.option', 2, 'Child seat');
        $ch_seat_types[] = JHTML::_('select.option', 3, 'Booster seat');
        $lists['chseat_type'] = JHTML::_('select.genericlist', $ch_seat_types, 'chseat_type', 'title="Do you need any child seats?"', 'value', 'text', $chseat_type);

        $suitcases_options = array();
        for ($i = 0; $i <= 9; $i++)
        {
            $suitcases_options[] = JHTML::_('select.option', $i, $i);
        }
        $lists['suitcases'] = JHTML::_('select.genericlist', $suitcases_options, 'suitcases', 'onChange="check_cases(this.value)"', 'value', 'text', $suitcases);

        return $lists;
    }

}

?>